

let State = require('./state');

// let AddSelCmd = require('../cmds/addSelCmd');
// let MoveSelCmd = require('../cmds/moveSelCmd');

let AddSelCommand = require('../cmds/addSelCmd');
let MoveSelCommand = require('../cmds/moveSelCmd');

// 编辑轨道
// 编辑轨道的时候, 桥或者洞不可以选中
class EditChainState extends State {

    constructor(){
        super();
        // this._moved = false;
        // this._beginPos = null;
    }

    onEnter() {
        super.onEnter();
        this.hideDeco();
        this.hideBattery();
    }

    touchBegin(event) {

        console.log('touchBegin called!');

        let mapScene = editor.gameApp;
        let wpos = event.getLocation();
        let pos = mapScene.node.convertToNodeSpaceAR(wpos);

        let sel = mapScene.pickSel(pos);
        if (sel) {
            cc.log('点击 sel.....');
            let selnode = sel.node;
            this._offx = pos.x - selnode.x;
            this._offy = pos.y - selnode.y;
            mapScene.setCurSel(sel);
        } else {
            // let chainIdx = mapScene.getChainIdx(mapScene.curChain);
            // let cmd = new AddSelCmd(chainIdx,pos);
            // cmd.execute();

            let cmd = new AddSelCommand(mapScene.curChain,pos);
            editor.execute(cmd);
        }

        //
        // let curSel = mapScene.curSel;
        // if(curSel){
        //     this._beginPos = cc.v2(curSel.node.x,curSel.node.y);
        // }
    }

    touchMove(event) {
        console.log('touchMove called....');
        let mapScene = editor.gameApp;
        let wpos = event.getLocation();
        let pos = mapScene.node.convertToNodeSpaceAR(wpos);
        pos.x -= this._offx;
        pos.y -= this._offy;
        let sel = mapScene.curSel;
        // mapScene.curChain.moveSel(sel,pos);

        let moveCmd = new MoveSelCommand(sel,pos);
        editor.execute(moveCmd);

        // this._moved = true;
    }

    touchEnd(event) {
        console.log('touch end called!');

        // let gameApp = G.gameApp;
        // if(this._moved && gameApp.curSel && this._beginPos){
        //     let curPos = cc.v2();
        //     gameApp.curSel.node.getPosition(curPos);
        //
        //     let chainIdx = gameApp.getChainIdx(gameApp.curChain);
        //     let moveSelCmd = new MoveSelCmd(chainIdx,gameApp.curSel.getIdx(),this._beginPos,curPos);
        //     moveSelCmd.execute();
        //     this._beginPos = null;
        // }
        // this._moved = false;
    }

    touchCancel(event) {
        console.log('touch cancel called!');
        // this._moved = false;
        // this._beginPos = null;
    }
}


module.exports = EditChainState;
